package com.info.controller;

import com.info.model.Course;
import com.info.service.CourseService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    //    public void setCourseService(CourseService courseService) {
//        this.courseService = courseService;
//    }
    //后台输出日志，查看
    private static Logger log = LoggerFactory.getLogger(CourseController.class);

    //本方法将处理 /courses/view?courseId=123 形式的URL
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String viewCourse(@RequestParam("courseId") Integer courseId, Model model) {
        log.debug("In viewCourse, courseId = {}", courseId);
        Course course = courseService.getCourseById(courseId);
        model.addAttribute(course);
        return "courseView";
    }

    //本方法将处理 /courses/view/123 形式的URL
    @RequestMapping(value = "/view2/{courseId}", method = RequestMethod.GET)
    public String viewCourse2(@PathVariable("courseId") Integer courseId, Map<String, Object> model) {
        Course course = courseService.getCourseById(courseId);
        model.put("course", course);

        return "courseView";
    }

    //使用HttpServletRequest,处理 /courses/view3?courseId=123 形式的URL
    @RequestMapping("/view3")
    public String viewCourse3(HttpServletRequest request) {
        Integer courseId = Integer.valueOf(request.getParameter("courseId"));
        Course course = courseService.getCourseById(courseId);
        request.setAttribute("course", course);
        return "courseView";
    }

    //访问编辑页面入口,本方法访问形式：/courses/admin?add
    @RequestMapping(value = "/admin", method = RequestMethod.GET, params = "add")
    public String creatCourse() {
        return "course_admin/edit";
    }

    /*
    用来提交edit页面的表单数据
    * */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String doSave(@ModelAttribute Course course) {
        log.debug("Info of Course:");
        log.debug(ReflectionToStringBuilder.toString(course));

        //拦截表单数据，没有数据库，实例指定id
        course.setCourseId(123);
        return "redirect:view2/" + course.getCourseId();
    }

    //访问文件上传入口,访问方式：/courses/upload
    @RequestMapping(value = "/upload", method = RequestMethod.GET)
    public String showUploadPage() {
        return "course_admin/file";
    }
    //上传成功后，进入success.jsp页面
    @RequestMapping(value = "/doUpload", method = RequestMethod.POST)
    public String doUploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        //@RequestParam("file") MultipartFile file: 文件自动binding file.jsp页面上，name为file的控件
        if (!file.isEmpty()) {
            log.debug("procee file:{}", file.getOriginalFilename());
            //获取文件，存储到新位置，文件名：当前时间+原文件名
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File("D:\\test\\", System.currentTimeMillis() + file.getOriginalFilename()));
        }
        return "success";
    }

    //SpringMvc与Json协同工作：SpringMvc使用ViewResolver来处理，相同数据不同应用场景的数据显示。
    // 方式一通过注解，访问方式：http://localhost:8080/SpringMvcDemo/courses/123

    @RequestMapping(value="/{courseId}",method=RequestMethod.GET)
    public @ResponseBody Course getCourseInJson(@PathVariable Integer courseId){

        return  courseService.getCourseById(courseId);
    }

    //方式二 返回实体类型，访问方式：http://localhost:8080/SpringMvcDemo/courses/jsontype/1234
    @RequestMapping(value="/jsontype/{courseId}",method=RequestMethod.GET)
    public ResponseEntity<Course> getCourseInJson2(@PathVariable Integer courseId){
        Course course = courseService.getCourseById(courseId);
        return new ResponseEntity<Course>(course, HttpStatus.OK);
    }

}

